<?php
//рус
class templator_class extends module_related_base_class
{
    private $_libs;
    
    /**
 		базовый путь к шаблонам
     */
    private $_baseTmpPath;
    
    /**
		данные для обработки
     */
    private $_data = array();
    private $_isCache = true;
    
    function __construct ($module, $basePath)
    {
    	parent::__construct($module);
        $this->SetBasePath($basePath);
    }
    
    protected function Libs()
    {
    	if (!$this->_libs)	$this->_libs = SysLoader::GetLibCollection(); 
    	return $this->_libs;   	
    }    
    
    /**
	* вкл/выкл кэш шаблонов
	*/
    public function CacheTurnOff()
    {
        $this->_isCache = false;
        return $this;        
    }

    public function CacheTurnOn()
    {
        $this->_isCache = true;  
        return $this;      
    }    
    
    /**
		устанавливает базовый путь к шаблонам
     */
    public function SetBasePath ($path = '')
    {
        if (strlen($path) && ! is_dir($path))
            throw new Exception('Путь не найден');
        $this->_baseTmpPath = (string)$path;
        return $this;
    }
    /**
		возвращает базовый путь к шаблонам
     */
    public function GetBasePath ()
    {
        return $this->_baseTmpPath;
    }
    private function __set ($name, $value)
    {
        $this->CheckVar($name);
        $this->_data[$name] = $value;
    }
    private function &__get ($name)
    {
        $this->CheckVar($name);
        return $this->_data[$name];
    }
    private function __isset ($name)
    {
        $this->CheckVar($name);
        return isset($this->_data[$name]);
    }
    private function __unset ($name)
    {
        $this->CheckVar($name);
        if ($this->__isset($this->_data[$name]))
            unset($this->_data[$name]);
    }
    private function CheckVar ($name)
    {
        if (property_exists(__CLASS__, $name))
            throw new Exception('Доступ к закрытому свойству класса ' . __CLASS__ . ' запрещён');
        return true;
    }
    public function render ($fileName)
    {   
        ob_start();
        include($this->GetRealFile($fileName));
        return ob_get_clean(); // filter output
    }
    
    private function CleanContent($content)
    {
	    //return str_replace("\t", '', $content); 
        return $this->Libs()->minifier->MinifyHTML($content); 
    }

    private function GetRealFile($fileName)
    {
        $basePath = $this->GetBasePath();
        if (0 === strpos($basePath, _ROOT_PATH)) $basePath = substr($basePath, strlen(_ROOT_PATH));
        
        if (!MozgCfg::GetProp('global//isTmpCache') || !$this->_isCache) return $this->GetOriginalFile($fileName);
                
        $tmpCachePath = MozgCfg::GetProp('global//tmpCachePath').$basePath;
        
        if (!is_file($tmpCachePath.$fileName))
        {
            if ($this->Libs()->dir->Create($tmpCachePath.pathinfo($fileName, PATHINFO_DIRNAME)))
            {
                file_put_contents($tmpCachePath.$fileName, $this->CleanContent(file_get_contents($this->GetOriginalFile($fileName))));                            
            }
        } 

        return $tmpCachePath.$fileName;
    }
    
    private function GetOriginalFile($fileName)
    {
        $fullPath = $this->GetBasePath().$fileName;
		if (!is_file($fullPath)) throw new Exception('Шаблон ' . $fileName . ' не найден');
		return $fullPath;         
    }
    
    private $_style;
    
    public function Style()
    {
    	if (!$this->_style)
    	{
    		$this->_style = SysLoader::GetModule('media', 'http', '_web')->GetCtrl('style');    	
    	}
    	
    	return $this->_style;
    }
}